package main

import (
	"context"
	"flag"
	"fmt"

	"github.com/ory/ladon"

	metav1 "gitee.com/youkelike/ziam/zapi/meta/v1"
	authzclientv1 "gitee.com/youkelike/ziam/zsdk2/gosdk/service/ziam/authz/v1"
	"gitee.com/youkelike/ziam/zsdk2/rest"
	"gitee.com/youkelike/zlog"
)

func main() {
	authzconfig := flag.String("config", `D:\goproject\ziam\config\zsdk2-authz.yaml`, "absolute path to the config file")
	flag.Parse()
	zlog.Infow("===", zlog.String("authzconfig", *authzconfig))

	config, err := rest.ConfigFromFile(*authzconfig)
	if err != nil {
		panic(err.Error())
	}
	// zlog.Infow("===", zlog.Any("config", *config))

	client, err := authzclientv1.NewForConfig(config)
	if err != nil {
		panic(err.Error())
	}

	request := &ladon.Request{
		Resource: "resources:articles:ladon-introduction",
		Action:   "delete",
		Subject:  "users:maria",
		Context: ladon.Context{
			"remoteIPAddress": "192.168.0.5",
		},
	}

	fmt.Println("Authorize request...")

	ret, err := client.Authz().Authorize(context.TODO(), request, metav1.AuthorizeOptions{})
	if err != nil {
		panic(err.Error())
	}

	fmt.Printf("Authorize response: %s.\n", ret.ToString())
}
